define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree', 'vue','template'], function ($, undefined, Backend, Table, Form, undefined, Vue,Template) {
//读取选中的条目
    function initSearchVue() {
        var category_vue1 = new Vue({
            el: '#category_div1',
            data: {
                category1_list: [],
                category_id1: 0,
            }
        });
        var category_vue2 = new Vue({
            el: '#category_div2',
            data: {
                category2_list: [],
                category_id2: 0,
            }
        });
        var category_vue3 = new Vue({
            el: '#category_div3',
            data: {
                category3_list: [],
                category_id3: 0,
            }
        });
        var category_vue4 = new Vue({
            el: '#category_div4',
            data: {
                category4_list: [],
                category_id4: 0,
            }
        });
        var category_vue5 = new Vue({
            el: '#category_div5',
            data: {
                category5_list: [],
                category_id5: 0,
            }
        });
        var category_vue6 = new Vue({
            el: '#category_div6',
            data: {
                category6_list: [],
                category_id6: 0,
            }
        });
        var category_vue7 = new Vue({
            el: '#category_div7',
            data: {
                category7_list: [],
                category_id7: 0,
            }
        });
        $('select[name*=category_id1]').on('change', function () {
            category_vue2.category_id2 = 0;
            category_vue3.category_id3 = 0;
            category_vue4.category_id4 = 0;
            category_vue5.category_id5 = 0;
            category_vue6.category_id6 = 0;
            category_vue7.category_id7 = 0;
            category_vue2.category2_list = [];
            category_vue3.category3_list = [];
            category_vue4.category4_list = [];
            category_vue5.category5_list = [];
            category_vue6.category6_list = [];
            category_vue7.category7_list = [];
            initSubCategoryList(this.value,1);
        });
        $('select[name*=category_id2]').on('change', function () {
            category_vue3.category_id3 = 0;
            category_vue4.category_id4 = 0;
            category_vue5.category_id5 = 0;
            category_vue6.category_id6 = 0;
            category_vue7.category_id7 = 0;
            category_vue3.category3_list = [];
            category_vue4.category4_list = [];
            category_vue5.category5_list = [];
            category_vue6.category6_list = [];
            category_vue7.category7_list = [];
            initSubCategoryList(this.value,2);
        });
        $('select[name*=category_id3]').on('change', function () {
            category_vue4.category_id4 = 0;
            category_vue5.category_id5 = 0;
            category_vue6.category_id6 = 0;
            category_vue7.category_id7 = 0;
            category_vue4.category4_list = [];
            category_vue5.category5_list = [];
            category_vue6.category6_list = [];
            category_vue7.category7_list = [];
            initSubCategoryList(this.value,3);
        });
        $('select[name*=category_id4]').on('change', function () {
            category_vue5.category_id5 = 0;
            category_vue6.category_id6 = 0;
            category_vue7.category_id7 = 0;
            category_vue5.category5_list = [];
            category_vue6.category6_list = [];
            category_vue7.category7_list = [];
            initSubCategoryList(this.value,4);
        });
        $('select[name*=category_id5]').on('change', function () {
            category_vue6.category_id6 = 0;
            category_vue7.category_id7 = 0;
            category_vue6.category6_list = [];
            category_vue7.category7_list = [];
            initSubCategoryList(this.value,5);
        });
        $('select[name*=category_id6]').on('change', function () {
            category_vue7.category_id7 = 0;
            category_vue7.category7_list = [];
            initSubCategoryList(this.value,6);
        });
        function initSubCategoryList(parent_id, parent_level) {
            var data = {'parent_id': parent_id};
            $.get('adminpublic/ajax_category_list', data, function (res) {
                switch (parent_level) {
                    case 0:
                        category_vue1.category1_list = res;
                        break;
                    case 1:
                        category_vue2.category2_list = res;
                        break;
                    case 2:
                        category_vue3.category3_list = res;
                        break;
                    case 3:
                        category_vue4.category4_list = res;
                        break;
                    case 4:
                        category_vue5.category5_list = res;
                        break;
                    case 5:
                        category_vue6.category6_list = res;
                        break;
                    case 6:
                        category_vue7.category7_list = res;
                        break;
                }
            });
        }
        initSubCategoryList(0,0);
    }
    
    function initVue() {
        var vue = new Vue({
            el: '#form_div',
            data: {
                category1_list: [],
                category2_list: [],
                category3_list: [],
                category4_list: [],
                category5_list: [],
                category6_list: [],
                category7_list: [],
                category_id1: 0,
                category_id2: 0,
                category_id3: 0,
                category_id4: 0,
                category_id5: 0,
                category_id6: 0,
                category_id7: 0,
                name: '',
                the_latin_name: '',
                amount: 0,
                demand: 0
            },
            watch: {
                category_id1: function (val) {
                    this.category_id1 = val;
                    this.category2_list = [];
                    this.category3_list = [];
                    this.category4_list = [];
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 1);
                },
                category_id2: function (val) {
                    this.category_id2 = val;
                    this.category3_list = [];
                    this.category4_list = [];
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 2);
                },
                category_id3: function (val) {
                    this.category_id3 = val;
                    this.category4_list = [];
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 3);
                },
                category_id4: function (val) {
                    this.category_id4 = val;
                    this.category5_list = [];
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 4);
                },
                category_id5: function (val) {
                    this.category_id5 = val;
                    this.category6_list = [];
                    this.category7_list = [];
                    this.initSubCategoryList(val, 5);
                },
                category_id6: function (val) {
                    this.category_id6 = val;
                    this.category7_list = [];
                    this.initSubCategoryList(val, 6);
                }
            },
            methods: {
                initCategoryList: function () {
                    this.initSubCategoryList(0, 0);
                },
                initSubCategoryList: function (parent_id, parent_level) {
                    var data = {'parent_id': parent_id};
                    var that = this;
                    $.get('adminpublic/ajax_category_list', data, function (res) {
                        switch (parent_level) {
                            case 0:
                                that.category1_list = res;
                                break;
                            case 1:
                                that.category2_list = res;
                                break;
                            case 2:
                                that.category3_list = res;
                                break;
                            case 3:
                                that.category4_list = res;
                                break;
                            case 4:
                                that.category5_list = res;
                                break;
                            case 5:
                                that.category6_list = res;
                                break;
                            case 6:
                                that.category7_list = res;
                                break;
                        }
                    });
                }
            }
        });
        return vue;
    }

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'repository/bacteria',
                    add_url: 'repository/bacteria/add',
                    edit_url: 'repository/bacteria/edit',
                    del_url: 'repository/bacteria/del'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                search: false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'chinese_name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {
                            field: 'the_latin_name',
                            title: __('The latin name'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索'
                        },
                        {field: 'demand', title: __("Demand"), searchable: false},
                        {field: 'amount', title: __("Amount"), searchable: false},
                        {field: 'in_review', title: __("In review"), searchable: false},
                        {field: 'category_id1', title: __('Level category1'),visible:false,searchList: function (column) {
                            return Template('category_search1', {});}
                        },
                        {field: 'category_id2', title: __('Level category2'),visible:false,searchList: function (column) {
                            return Template('category_search2', {});}
                        },
                        {field: 'category_id3', title: __('Level category3'),visible:false,searchList: function (column) {
                            return Template('category_search3', {});}
                        },
                        {field: 'category_id4', title: __('Level category4'),visible:false,searchList: function (column) {
                            return Template('category_search4', {});}
                        },
                        {field: 'category_id5', title: __('Level category5'),visible:false,searchList: function (column) {
                            return Template('category_search5', {});}
                        },
                        {field: 'category_id6', title: __('Level category6'),visible:false,searchList: function (column) {
                            return Template('category_search6', {});}
                        },
                        {field: 'category_id7', title: __('Level category7'),visible:false,searchList: function (column) {
                            return Template('category_search7', {});}
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'verify_log',
                                    text: __('Verify log'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: 'repository/bacteria/verify_log'
                                }
                            ],
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            var url = Config.site.cdnurl + '/index.php/admin/repository/bacteria/import';
            // initFileInput(url);
            $(document).on("click", ".btn-import", function () {
                $('#uploadModal').modal();

            });
            initSearchVue();
            $(document).on("click", "#upload_btn", function () {
                var form = new FormData(document.getElementById("upload_form"));
                $.ajax({
                    url: Config.site.cdnurl + '/index.php/admin/repository/bacteria/import',
                    type: "POST",
                    data: form,
                    processData: false,
                    contentType: false,
                    success: function (data) {
                        if (data.code == 1) {
                            Toastr.success(data.msg);
                            $('#uploadModal').modal('hide');
                        } else {
                            Toastr.error(data.msg);
                        }
                    },
                    error: function (e) {
                    }
                });
            });
        },
        edit: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initVue();
            var bacteria = Config.bacteria;
            console.info(bacteria);
            vue.name = bacteria.chinese_name;
            vue.amount = bacteria.amount;
            vue.demand = bacteria.demand;
            vue.the_latin_name = bacteria.the_latin_name;
            vue.english_name = bacteria.english_name;
            vue.category_id1 = bacteria.category_id1;
            vue.category_id2 = bacteria.category_id2;
            vue.category_id3 = bacteria.category_id3;
            vue.category_id4 = bacteria.category_id4;
            vue.category_id5 = bacteria.category_id5;
            vue.category_id6 = bacteria.category_id6;
            vue.category_id7 = bacteria.category_id7;
            vue.initCategoryList();
        },
        add: function () {
            Form.api.bindevent($("form[role=form]"));
            var vue = initVue();
            vue.initCategoryList();
        },
        verify_log: function () {
            var bacteria_id = Config.bacteria_id;
            Table.api.init({
                extend: {
                    index_url: 'repository/bacteria/verify_log/ids/' + bacteria_id
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'repository/bacteria/verify_log/ids/' + bacteria_id,
                search: false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'member_name', title: __('Member name'), operate: 'LIKE %...%', placeholder: '模糊搜索'},
                        {
                            field: 'type',
                            title: __('type'),
                            formatter: function (value, row, index) {
                                value = '' + value;
                                switch (value) {
                                    case '0':
                                        return __('change');
                                    case '1':
                                        return __('push');
                                }
                            },
                            searchList: {'0': __('change'), '1': __('push')},
                            style: 'min-width:100px;'
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            formatter: function (value, row, index) {
                                value = '' + value;
                                switch (value) {
                                    case '0':
                                        return __('Wait');
                                    case '1':
                                        return __('Verify passed');
                                    case '2':
                                        return __('Verify fail');
                                    default:
                                        return __('Wait');
                                }
                            },
                            searchList: {'0': __('Wait'), '1': __('Verify passed'), '2': __('Verify fail')},
                            style: 'min-width:100px;'
                        },
                        {field: 'desc', title: __('Verify desc'), searchable: false},
                        {
                            field: 'create_time',
                            title: __("Createtime"),
                            formatter: Table.api.formatter.datetime,
                            searchable: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        login_log: function () {
            var member_id = Config.member_id;
            Table.api.init({
                extend: {
                    index_url: 'user/member_list/login_log/ids/' + member_id
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/member_list/login_log/ids/' + member_id,
                search: false,
                columns: [
                    [
                        {field: 'id', title: 'ID'},
                        {field: 'name', title: __('Name'), searchable: false},
                        {field: 'ip', title: __("Ip"), searchable: false},
                        {
                            field: 'create_time',
                            title: __("Createtime"),
                            formatter: Table.api.formatter.datetime,
                            searchable: false
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                return Table.api.formatter.operate.call(this, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        verify_num: function () {
            var member_id = Config.member_id;
            Table.api.init({
                extend: {
                    index_url: 'user/member_list/verify_num/ids/' + member_id
                }
            });
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'user/member_list/verify_num/ids/' + member_id,
                search: false,
                columns: [
                    [
                        {field: 'verify_success', title: __("Verify success"), searchable: false},
                        {field: 'verifying', title: __("Verifying"), searchable: false},
                        {field: 'verify_fail', title: __("Verify fail"), searchable: false}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        }
    };
    return Controller;
});